
/**
 * Servlet implementation class XHRServlet
 */
import java.io.IOException;
import java.io.Serializable;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

@WebServlet("/XHRServlet")
public class XHRServlet extends HttpServlet implements Serializable {
    // 序列化版本号，作用是在反序列化时检查类的版本是否一致
    private static final long serialVersionUID = 1L;
  
    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        response.setContentType("text/html;charset=utf-8");
        // 新闻标题数组对应颜色
        String[] news = {"新闻1", "新闻2", "新闻3"};
        String[] colors = {"pink", "lightgreen", "orange"};
        // 随机生成一个新闻标题   
        int index = (int) (Math.random() * news.length);
        String title = news[index];
        String color = colors[index];
        // 返回新闻标题   
        response.getWriter().write("<h3 style=\"color=" + color + "\">" + title + "</h3>");
    }
}